//
//  MainTableViewController.m
//  VideoDiaryClass
//
//  Created by Wang Haitao on 15/10/19.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "MainTableViewController.h"
#import "DiaryFile.h"
#import "MainCellBase.h"
#import "UIViewAdditions.h"
#import "NSString+Frame.h"

@interface MainTableViewController ()

@end

@implementation MainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[DiaryFile sharedInstance] diaryCount];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Diary * diary = [[DiaryFile sharedInstance] diaryAtIndex:indexPath.row];

    float contentWidth = self.view.width/2 - 5 - 16;
    float contentHeight = [diary.content heightWithFont:[UIFont systemFontOfSize:17] withinWidth:contentWidth];
    
    if (contentHeight<112-8-37) {
        contentHeight = 112;
    }
    else {
        contentHeight += 8 + 37;
    }
    
    return contentHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Diary * diary = [[DiaryFile sharedInstance] diaryAtIndex:indexPath.row];
    BOOL leftCell = diary.type==0;
    
    MainCellBase *cell = [tableView dequeueReusableCellWithIdentifier: leftCell?@"LeftCellID":@"RightCellID" forIndexPath:indexPath];
    
    // Configure the cell...
    
    cell.diary = diary;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    Diary * diary = [[DiaryFile sharedInstance] diaryAtIndex:indexPath.row];

    AVPlayerViewController * playerViewController = [AVPlayerViewController new];
    playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:diary.videoPath]];
    
    [self presentViewController:playerViewController animated:YES completion:nil];
}

#pragma mark -- 允许手机横评
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertController * ac = [UIAlertController alertControllerWithTitle:nil message:@"Are U sure delete this video diary?" preferredStyle:UIAlertControllerStyleAlert];
        [ac addAction:[UIAlertAction actionWithTitle:@"Delete!" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [[DiaryFile sharedInstance] removeDiaryAtIndex:indexPath.row
             ];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }]];
        [ac addAction:[UIAlertAction actionWithTitle:@"Give up" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:ac animated:YES completion:nil];
    }
}

- (IBAction)unwindBack:(UIStoryboardSegue *)unwindSegue
{
    [self.tableView reloadData];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

@end
